package com.bytedance.common.utility;

import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;

public final class aa {
    private static char[] a;

    static {
        aa.a = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    }

    public static String a(File arg6) {
        String v0 = null;
        try {
            MessageDigest v1_1 = MessageDigest.getInstance("MD5");
            if(v1_1 != null) {
                FileInputStream v2 = new FileInputStream(arg6);
                byte[] v3 = new byte[8192];
                while(true) {
                    int v4 = v2.read(v3, 0, 8192);
                    if(v4 <= 0) {
                        break;
                    }

                    v1_1.update(v3, 0, v4);
                }

                v2.close();
                v0 = b(v1_1.digest());
            }

            return v0;
        }
        catch(Exception v1) {
            return v0;
        }
    }

    public static String a(byte[] arg2) {
        String v0 = null;
        if(arg2 != null) {
            try {
                if(arg2.length == 0) {
                    return v0;
                }

                MessageDigest v1_1 = MessageDigest.getInstance("MD5");
                v1_1.update(arg2);
                v0 = b(v1_1.digest());
            }
            catch(Exception v1) {
            }
        }

        return v0;
    }

    public static String a(byte[] bytes, int arg9) {
        if(bytes == null) {
            throw new NullPointerException("bytes is null");
        }

        if(arg9 > bytes.length) {
            throw new IndexOutOfBoundsException();
        }

        char[] v3 = new char[arg9 << 1];
        int v0 = 0;
        int v2 = 0;
        while(v0 < arg9) {
            int v4 = bytes[v0] & 255;
            int v5 = v2 + 1;
            v3[v2] = a[v4 >> 4];
            v2 = v5 + 1;
            v3[v5] = a[v4 & 15];
            ++v0;
        }

        return new String(v3, 0, arg9 << 1);
    }

    public static byte[] a(String arg8) throws IllegalArgumentException {
        int v7 = 16;
        if(arg8 != null && arg8.length() % 2 != 1) {
            char[] v1 = arg8.toCharArray();
            int v2 = v1.length;
            byte[] v3 = new byte[v2 / 2];
            int v0;
            for(v0 = 0; v0 < v2; v0 += 2) {
                v3[v0 / 2] = ((byte)((Character.digit(v1[v0], v7) << 4) + Character.digit(v1[v0 + 1], v7)));
            }

            return v3;
        }

        throw new IllegalArgumentException("hexBinary needs to be even-length: " + arg8);
    }

    private static String b(byte[] arg2) {
        if(arg2 == null) {
            throw new NullPointerException("bytes is null");
        }

        return a(arg2, arg2.length);
    }

    public static String md5(String input) {
        String v0 = null;
        if(input != null) {
            try {
                if(input.length() == 0) {
                    return v0;
                }
                MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                messageDigest.update(input.getBytes("UTF-8"));
                return b(messageDigest.digest());
            }
            catch(Exception v1) {
            }
        }

        return v0;
    }
}

